package com.cpkso.yzx.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cpkso.yzx.domain.Info;
import com.cpkso.yzx.domain.Msg;
import com.cpkso.yzx.domain.Qq;
import com.cpkso.yzx.dto.MsgQuery;
import com.cpkso.yzx.repository.InfoRepository;
import com.cpkso.yzx.repository.MsgRepository;
import com.cpkso.yzx.repository.QqRepository;

import runze.util.base.IService;
import runze.util.base.Response;

/**
 * 管理联系方式，qq， 地址信息等
 * @author wrzhxy@qq.com
 * @date 2017年11月9日
 */
@Service
@Transactional
public class ContactService implements IService<Qq> {
	/**
	 * 前台功能
	 */
	public Response showqq() {
		Response response = makeResponse();
		List<Qq> data = qqRepository.find();
		response.setData(data);
		return response;
	}
	public Response showinfo() {
		Response response = makeResponse();
		Info data = infoRepository.get(1);
		response.setData(data);
		return response;
	}
	public long leaveMsg(Msg msg) {
		if (msg == null) return 0;
		
		msg.setAddress(transfer(msg.getAddress()));
		msg.setBrief(transfer(msg.getBrief()));
		msg.setCourseName(transfer(msg.getCourseName()));
		msg.setCurtime(new Date());
		msg.setEmail(transfer(msg.getEmail()));
		msg.setName(transfer(msg.getName()));
		msg.setTel(transfer(msg.getTel()));
		
		return msgRepository.add(msg);
	}
	
	/**
	 * 后台功能
	 */
	public long addQq(Qq qq) {
		return qqRepository.add(qq);
	}
	public void delQq(long id) {
		qqRepository.del(id);
	}
	/**
	 * @param qq
	 */
	public void modifyQq(Qq qq) {
//		Qq qq_db = null;
//		try {
//			qq_db = qqRepository.get(qq.getId());
//		} catch (NullPointerException e) {
//			e.printStackTrace();
//			return;
//		}
//		String name = qq.getName();
//		if (name == null) qq_db.setName(name);
//		String qq_str = qq.getQq();
//		if (qq_str != null) qq_db.setQq(qq_str);
		
		qqRepository.update(qq);
	}
	
	public long addInfo(Info info) {
		return infoRepository.add(info);
	}
	public void delInfo(long id) {
		infoRepository.del(id);
	}
	public void modifyInfo(Info info) {
		infoRepository.update(info);
	}
	
	public Response showMsgs(MsgQuery query) {
		if (query == null)
			query = new MsgQuery();
		
		Response response = makeResponse();
		List<Msg> data = msgRepository.find(query);
		response.setData(data);
		query.setMaxPageNo(msgRepository.getTotal(query));
		response.setQuery(query);
		return response;
	}
	public void delMsg(long id) {
		msgRepository.del(id);
	}
	public void modifyMsg(Msg msg) {
		msgRepository.update(msg);
	}
	/**
	 * 查看详情 并设置消息已读
	 * @param id
	 * @return Msg
	 */
	public Response showMsg(long id) {
		Response response = makeResponse();
		Msg data = msgRepository.get(id);
		data.setRead(1);
		response.setData(data);
		return response;
	}
	// 设置已读
	public int read() {
		return msgRepository.read();
	}
	
	private String transfer(String value) {
		if (value == null) return null;
			value = value.trim()
			.replaceAll("<", "&lt;")
			.replaceAll(">", "&gt;")
			.replaceAll("&", "&amp;")
			.replaceAll("\"", "&quot;")
			.replaceAll("\'", "&#39;");
		return value;
	}
	
	private QqRepository qqRepository;
	private InfoRepository infoRepository;
	private MsgRepository msgRepository;
	@Autowired
	public void setQqRepository(QqRepository qqRepository) {
		this.qqRepository = qqRepository;
	}
	@Autowired
	public void setInfoRepository(InfoRepository infoRepository) {
		this.infoRepository = infoRepository;
	}
	@Autowired
	public void setMsgRepository(MsgRepository msgRepository) {
		this.msgRepository = msgRepository;
	}
	
}
